﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Ink;
using System.Globalization;
using System.Windows.Markup;
using System.Windows.Media.Animation;

using ZOIL.DataModel;
using ZOIL.Framework.Presentation;
using Microsoft.Surface.Presentation;

using System.Reflection;

namespace ZOIL.Library.Components//Library.Components.ViPostIt
{
    /// <summary>
    /// Interaction logic for ViPostItView.xaml
    /// </summary>
    /// 
    [ViewFor(typeof(ViPostIt), "default")]
    public partial class ViPostItView : ZComponent//UserControl
    {
        private static Random _rnd = new Random();
        //lars hack
        private static string assemblyPath = System.IO.Path.GetDirectoryName(
                     Assembly.GetAssembly(typeof(ViPostItView)).CodeBase);

        private string YELLOWPOSTIT = assemblyPath+"/../../Images/PostItYellow.jpg";
        private string BLUEPOSTIT = assemblyPath+"/../../Images/PostItBlue.jpg";
        private string PINKPOSTIT = assemblyPath + "/../../Images/PostItPink.jpg";
        private string GREENPOSTIT = assemblyPath + "/../../Images/PostItGreen.jpg";
        private BitmapImage yellowPostIt;
        private BitmapImage bluePostIt;
        private BitmapImage pinkPostIt;
        private BitmapImage greenPostIt;
        private bool colorPressed = false;
        ViPostItViewModel _viewModel;
        /// <summary>
        /// The most recent stroke (used for undo operations).
        /// </summary>
        private Stroke mostRecentStroke;

        public ViPostItView()
        {
            
            InitializeComponent();
            //InitializeImages();
            //step below is necessary otherwise _viewModel would be null;
            this.Loaded += new RoutedEventHandler(ViPostItView_Loaded);
            
        }

        void ViPostItView_Loaded(object sender, RoutedEventArgs e)
        {
            _viewModel = this.DataContext as ViPostItViewModel;
        }

        private void ZComponent_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //((Storyboard)FindResource("MouseDownStoryBoard")).Begin();
        }

        private void ZComponent_MouseUp(object sender, MouseButtonEventArgs e)
        {

            //((Storyboard)FindResource("MouseUpStoryBoard")).Begin();
        }

        private void HandleTabOnButton(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            e.Handled = true;
        }

        private void InitializeImages()
        {
            Uri uri = new Uri(YELLOWPOSTIT);
            this.yellowPostIt = new BitmapImage(uri);

            uri = new Uri(BLUEPOSTIT);
            this.bluePostIt = new BitmapImage(uri);

            uri = new Uri(PINKPOSTIT);
            this.pinkPostIt = new BitmapImage(uri);

            uri = new Uri(GREENPOSTIT);
            this.greenPostIt = new BitmapImage(uri);
        }

        #region Drawing Pad Specific Code

        //==========================================================//
        /// <summary>
        /// Handles the OnStrokeCollected event for SurfaceInkCanvas.
        /// </summary>
        /// <param name="sender">The SurfaceInkCanvas that raised the event.</param>
        /// <param name="args">The arguments for the event.</param>
        private void OnStrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs args)
        {
            mostRecentStroke = args.Stroke;
        }

        /// <summary>
        /// Handles the ContactDownEvent for the color wheel and the current color indicator.
        /// </summary>
        /// <param name="sender">The color wheel that raised the event.</param>
        /// <param name="args">The arguments for the event.</param>
        private void OnColorSelectionPreviewContactDown(object sender, ContactEventArgs args)
        {
            // Capture the contact and handle the event 
            IInputElement element = sender as IInputElement;
            if (element != null && Contacts.CaptureContact(args.Contact, element))
            {
                args.Handled = true;
            }
        }

        private void OnColorSelectionPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            IInputElement element = sender as IInputElement;
            if (element != null)
            {
                e.Handled = true;
            }
        }

        #endregion Drawing Pad Specific Code

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (_viewModel != null)
            {
                _viewModel.SmallUrl = new Uri(BLUEPOSTIT);// ViPostIt Komponente sollte Image Objekt beinhalten und kein string. 

                //// Open a Uri and decode a BMP image
                //Uri myUri = new Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute);
                //BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                //BitmapSource bitmapSource2 = decoder2.Frames[0];

                //// Draw the Image
                //Image myImage2 = new Image();
                //myImage2.Source = bitmapSource2;
                //myImage2.Stretch = Stretch.None;
                //myImage2.Margin = new Thickness(20);


            }
        }


        private void buttonGreen_Click(object sender, RoutedEventArgs e)
        {
            if (_viewModel != null)
            {
                _viewModel.SmallUrl = new Uri(GREENPOSTIT);
            }

        }

        private void buttonPink_Click(object sender, RoutedEventArgs e)
        {
            if (_viewModel != null)
            {
                _viewModel.SmallUrl = new Uri(PINKPOSTIT);
            }
        }

        private void buttonYellow_Click(object sender, RoutedEventArgs e)
        {
            if (_viewModel != null)
            {
                _viewModel.SmallUrl = new Uri(YELLOWPOSTIT);
            }
        }

        private void buttonColor_Click(object sender, RoutedEventArgs e)
        {
            if (!colorPressed)
            {
                button1.Visibility = Visibility.Visible;
                buttonGreen.Visibility = Visibility.Visible;
                buttonPink.Visibility = Visibility.Visible;
                buttonYellow.Visibility = Visibility.Visible;
                colorPressed = true;
            }
            else
            {
                button1.Visibility = Visibility.Hidden;
                buttonGreen.Visibility = Visibility.Hidden;
                buttonPink.Visibility = Visibility.Hidden;
                buttonYellow.Visibility = Visibility.Hidden;
                colorPressed = false;
            }
        }
    }
}
